var isValid = 0;
/* 标签操作 */
$('.student_tab')[0].onclick = function() {
	$('.student_tab')[0].parentElement.classList.add('current');
	$('.manager_tab')[0].parentElement.classList.remove('current');
	/* $('.login_line')[0].classList.remove('inManager'); */
	$("#login_r").hide();
	$("#login_l").show();
};
$('.manager_tab')[0].onclick = function() {
	$('.student_tab')[0].parentElement.classList.remove('current');
	$('.manager_tab')[0].parentElement.classList.add('current');
	/*	*/
	$("#login_l").hide();
	$("#login_r").show();
};
$('#student_login_btn').on('click', function() {

	if (!$("#login_user_form").valid()) {
		return false;
	}

	var type = "student";
	var account = $('form')[0].identify.value;
	var pwd = $('form')[0].student_pwd.value;
	login(type, account, pwd);
});
$('#manager_login_btn').on('click', function() {

	if (!$("#login_manager_form").valid()) {
		return false;
	}

	var type = "manager";
	var account = $('form')[1].account.value;
	var pwd = $('form')[1].manager_pwd.value;
	login(type, account, pwd);
});
function login(type, account, pwd) {
	var url = "./LoginServlet";
	$.ajax({
		"data" : "type=" + type + "&account=" + account + "&pwd=" + pwd,
		"url" : url,
		"type" : "POST",
		"success" : function(result) {
			var res = JSON.parse(result);
			if (res.status == 'error') {
				alert("密码错误，请重新登陆！");
				isValid = 0;
			}
			if (res.status == 'success') {
				location.href = res.value;
			}
		},
		"error" : function(result) {
			alert('请求超时，请重新登陆！');
			isValid = 0;
		}
	});
}
$(document).keydown(function(event) {
	if (event.keyCode == 13) {
		if ($('.current').find('a').attr("class") == 'student_tab')
			$('#student_login_btn').trigger('click');
		if ($('.current').find('a').attr("class") == 'manager_tab')
			$('#manager_login_btn').trigger('click');
	}
});

$(function() {
	$("#login_user_form").validate({
		rules : {
			student_pwd : {
				required : true,
				minlength : 6,
				maxlength : 20,
				isRightfulString : true,
			},
			identify : {
				required : true,
				isIdCardNo : true,
			},
		},
		onkeyup : false,
		focusCleanup : true,
		errorPlacement : function(error, element) {
			error.appendTo(element.parent("li").parent("ul").next("div"));
		},
		/* debug : true, */
		success : "valid"
	});

	$("#login_manager_form").validate({
		rules : {
			manager_pwd : {
				required : true,
				minlength : 6,
				maxlength : 20,
				isRightfulString : true,
			},
			account : {
				required : true,
				isRightfulString : true,
			}
		},
		onkeyup : false,
		focusCleanup : true,
		errorPlacement : function(error, element) {
			error.appendTo(element.parent("li").parent("ul").next("div"));
		},
		/* debug : true, */
		success : "valid"
	});
	$.validator.addMethod("isIdCardNo", function(value, element) {
		return this.optional(element) || isIdCardNo(value);
	}, "请正确输入您的身份证号码");
	$.validator.addMethod("isRightfulString", function(value, element) {
		return this.optional(element) || /^[A-Za-z0-9_-]+$/.test(value);
	}, "请输入合法字符");

});